package projetjava;


import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseAdapter;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import javax.xml.transform.Source;

/**
 *
 * @author Tom
 */
public class ApercuPhotoGrand extends JPanel{
    
    private BufferedImage apercu;
    private boolean zoom;
    /** Creates a new instance of Main */
    public ApercuPhotoGrand(ApercuAlbumVirtuel panneau){
        this.zoom=false;
        this.addMouseListener(new ControleurRetourJList(panneau));
    }
    public BufferedImage getApercu(){
        return this.apercu;
    }
    public void setApercu(BufferedImage bimg){
        this.apercu=bimg;
        this.repaint();
        this.revalidate();
    }
    
    
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        if (apercu == null)  return;
        
        int xApercu = apercu.getWidth();
        int yApercu = apercu.getHeight();
        int xPanel = this.getWidth();
        int yPanel = this.getHeight();
        Graphics2D g2 = (Graphics2D) g;
        // Si c'est cool
        if(xApercu < xPanel && yApercu < yPanel){
            g2.drawImage(apercu,(xPanel/2)-(xApercu/2),(yPanel/2)-(yApercu/2),xApercu,yApercu,this);
        }else{
            
            if(xApercu > xPanel && yApercu > yPanel){
                if(!zoom){
                    if(xApercu > yApercu)  g2.drawImage(apercu,0,(yPanel/2)-((int)(((double)xPanel/(double)xApercu)*(double)yApercu)/2),xPanel,(int)(((double)xPanel/(double)xApercu)*(double)yApercu),this);
                    if(xApercu < yApercu)  g2.drawImage(apercu,(xPanel/2)-((int)(((double)yPanel/(double)yApercu)*(double)xApercu)/2),0,(int)(((double)yPanel/(double)yApercu)*(double)xApercu),yPanel,this);
                    int val = Math.min(xPanel,yPanel);
                    if(xApercu == yApercu) g2.drawImage(apercu,Math.max(xPanel,yPanel)/2-Math.min(xPanel,yPanel)/2,Math.max(xPanel,yPanel)/2-Math.min(xPanel,yPanel)/2,Math.min(xPanel,yPanel),Math.min(xPanel,yPanel),this);
                }else{
                    g2.drawImage(apercu,(xPanel/2)-(xApercu/2),(yPanel/2)-(yApercu/2),xApercu,yApercu,this);
                }
                
            }else if(xApercu > xPanel){
                
                if(xApercu > yApercu)  g2.drawImage(apercu,0,0,xPanel,(int)(((double)xPanel/(double)xApercu)*(double)yApercu),this);
                if(xApercu < yApercu)  g2.drawImage(apercu,(xPanel/2)-((int)(((double)yPanel/(double)yApercu)*(double)xApercu)/2),0,(int)(((double)yPanel/(double)yApercu)*(double)xApercu),yPanel,this);
                if(xApercu == yApercu) g2.drawImage(apercu,0,(yPanel/2)-(Math.min(xApercu,xPanel)/2),xPanel,Math.min(xApercu,xPanel),this);
            }else if(yApercu > yPanel){
                
                if(xApercu > yApercu)  g2.drawImage(apercu,0,0,xPanel,(int)(((double)xPanel/(double)xApercu)*(double)yApercu),this);
                if(xApercu < yApercu)  g2.drawImage(apercu,(xPanel/2)-((int)(((double)yPanel/(double)yApercu)*(double)xApercu)/2),0,(int)(((double)yPanel/(double)yApercu)*(double)xApercu),yPanel,this);
                if(xApercu == yApercu) g2.drawImage(apercu,(xPanel/2)-(Math.min(xApercu,yPanel)/2),0,Math.min(xApercu,yPanel),yPanel,this);
            }
            
        }
        
    }
}


